package com.lyk.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.entity.A_timeRules;
import com.common.entity.PageBean;
import com.common.utils.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lyk.service.A_timeRulesService;
import com.lyk.service.impl.A_timeRulesServiceImpl;


public class A_timeRulesServlet extends HttpServlet {

	@Override
	public void init() throws ServletException {
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		String opr=request.getParameter("opr");
		if("update".equals(opr)){
			A_timeRulesService timeRulesService = new A_timeRulesServiceImpl();
			int num = 0;
			String errMsg = "";
			try{	
				Integer n = StringUtils.str2Integer(request.getParameter("timeRules_id"),0);
				if(n != null){
					A_timeRules timeRules = timeRulesService.findById(n);
					System.out.println(n+"111");
					timeRules.setTimeLimit(StringUtils.str2Integer(request.getParameter("timeLimit"),0));
					timeRules.setTimeRules_id(n);
					num = timeRulesService.changetimeRules(timeRules);
				}else{
					throw new RuntimeException("时间限制编号必须输入");
				}
			}catch(Exception e){
				e.printStackTrace();
				errMsg = e.getMessage();
			}
			PrintWriter out=resp.getWriter();
			if(num > 0 ){
				out.println("<script type='text/javascript'>alert('修改成功');location.href='timeRules.jsp';</script>");
			}else{
				out.println("<script type='text/javascript'>alert('修改失败');location.href='timeRules.jsp';</script>");
			}
		}else if("del".equals(opr)){
			A_timeRulesService timeRulesService = new A_timeRulesServiceImpl();
			int num = 0;
			String errMsg = "";
			try{
				Integer n = StringUtils.str2Integer(request.getParameter("timeRules_id"),0);
				if(n != null){
					num = timeRulesService.delA_adminer(n);
				}else{
					throw new RuntimeException("时间限制编号必须输入");
				}
			}catch(Exception e){
				e.printStackTrace();
				errMsg = e.getMessage();
			}
			PrintWriter out=resp.getWriter();
			if(num>0){
				out.println("<script type='text/javascript'>alert('删除成功');location.href='timeRules.jsp';</script>");
			}else{
				out.println("<script type='text/javascript'>alert('删除失败:需要删除该拍卖信息"+errMsg+"');history.back();</script>");
			}
		}else if("add".equals(opr)){
			A_timeRules timeRules = new A_timeRules();
			int rows=0;
			String errMsg="";
			try{
				timeRules.setTimeLimit(StringUtils.str2Integer(request.getParameter("timeLimit"), 0));
				A_timeRulesService timeRulesService = new A_timeRulesServiceImpl();
				rows=timeRulesService.savetimeRules(timeRules);
			}catch(Exception e){
				e.printStackTrace();
				errMsg=e.getMessage();
			}
			PrintWriter out=resp.getWriter();
			if(rows>0){
				out.println("<script type='text/javascript'>alert('新增成功');location.href='timeRules.jsp';</script>");
			}else{
				out.println("<script type='text/javascript'>alert('新增失败"+errMsg+"');history.back();</script>");
			}
		}else{
			
			listTime(request, resp);
		}
	}
	private void listTime(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		Map<String, Object> map=new HashMap<String, Object>();
		String timeRules_id = request.getParameter("timeRules_id");
		if(StringUtils.isNotNull(timeRules_id)){
			map.put("timeRules_id",timeRules_id);
		}
		A_timeRulesService timeRulesService = new A_timeRulesServiceImpl();
		Integer pageIndex=StringUtils.str2Integer(request.getParameter("pageIndex"), 1);
		Integer pageSize = StringUtils.str2Integer(request.getParameter("pageSize"), 3);
		PageBean<A_timeRules> pageBean = timeRulesService.findPageByCondition(pageIndex, pageSize, map);
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(pageBean);
		PrintWriter out=resp.getWriter();
		out.print(json);
	}
}
